{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "14962dd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.entities.tree_entity import TreeHelper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d6001d21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('read_product_data',))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd085370",
   "metadata": {},
   "source": [
    "### Tree class usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7e8ba3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialise the TreeHelper with the MarqueeID of the root Asset and the dataset to be used to build the tree.\n",
    "# In this example, we are building an STS index tree, so we use STS index datasets.\n",
    "\n",
    "tree = TreeHelper('MAM9TYYXXXXXXXXX', tree_underlier_dataset='STS_UNDERLIER_WEIGHTS', underlier_column='underlyingAssetId')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9069bde",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Builds the tree with the underlier dataset passed \n",
    "tree.build_tree() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b287a90",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Populate weights and attributions of the asset with the appropriate dataset and column name. \n",
    "# In this example, we use the datasets for STS indices.\n",
    "\n",
    "tree.populate_weights(dataset='STS_UNDERLIER_WEIGHTS', weight_column='weight')\n",
    "\n",
    "tree.populate_attribution(dataset='STS_UNDERLIER_ATTRIBUTION', attribution_column='absoluteAttribution')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79f3f22e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get tree as a df with the contents stored at this point\n",
    "tree.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Prints the tree structure formed by the Index for easy visualisation\n",
    "tree.get_visualisation(visualise_by='asset_name')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# This feature needs treelib to be installed.\n",
    "tree.get_visualisation(visualise_by='bbid')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "528eb668",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the time at which the tree was built\n",
    "tree.update_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e77945d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This feature needs the notebook package of gs-quant to be installed.\n",
    "# You can install it using 'pip install gs-quant[notebook]'\n",
    "tree.get_visualisation(visualise_by='bbid')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc17c69",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the time at which the tree was built\n",
    "tree.update_time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de5a16b6",
   "metadata": {},
   "source": [
    "### Accessing the child nodes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4a2dbce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Accessing the root node of the tree\n",
    "root_node = tree.root"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d13fdcd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Accessing the child nodes of the root node.\n",
    "child_nodes_depth_1 = root_node.direct_underlier_assets_as_nodes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8a15958",
   "metadata": {},
   "source": [
    "Each of these child nodes are TreeNode objects themselves, so their children can be accessed using the same technique. This can be used to recursively explore the full tree. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13230d97",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Accessing the child nodes of the first child node of the root.\n",
    "child_nodes_depth_2 = child_nodes_depth_1[0].direct_underlier_assets_as_nodes"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}